﻿$(function () {
    var items = [{
        css: "WikiPageLinkButton",
        text: "Add Wiki Page Link",
        action: function (btn) {
            // 'this' = jHtmlArea object
            // 'btn' = jQuery object that represents the <A> "anchor" tag for the Toolbar Button                    
            WikiPageLink(this);
        }
    }
    ,
    {        
        css: "WikiTocButton",
        text: "Add Wiki Page TOC",
        action: function (btn) {
            this.pasteHTML('{toc}');
        }
    }];
    AddEditorToolbarItem(items);
});

function WikiPageLink(editor) {
    $('#dialog-window').remove();
    // the following line relies on the admin theme.  dependencies[ dialog_container ]
    $('#dialog_container').html('<div id="dialog-window" title="Add Wiki Page Link"></div>');
    var root = $('#WebRoots_GetWebRoot').val();
    $.post(root + 'Admin/Pages/viWiki/Pages/Wiki.aspx', function (data) {
        $('#dialog-window').html(data);

        $('#WikiPagesContainer .WikiPagesList').hide();
        var value = $('#Namespaces').val();
        $('#WikiPagesContainer #' + value).show();

        var pageid = $('#WikiPagesContainer #' + value + ' .WikiPageItem:selected').val();
        var pagename = $('#WikiPagesContainer #' + value + ' .WikiPageItem:selected').text();
        $('#WikiPageIdSelection').text(pageid);
        $('#WikiPageLink').val(pagename);

        $('#dialog-window').dialog({
            modal: true,            
            resizable: false,
            height: 550,
            width: 500,
            overlay: { backgroundColor: "#000", opacity: 0.5 },
            buttons: {
                "Cancel": function () { $(this).dialog('close'); },
                "Save": function () {
                    $(this).dialog('close');
                    var wikipagelink = '[[' + $('#WikiPageLink').val() + '|' + $('#WikiPageIdSelection').text() + ']]'
                    editor.pasteHTML(wikipagelink);
                } //save
            }//buttons
        });
    });
}

function onWikiPageChanged() {
    var value = $('#Namespaces').val();
    var pageid = $('#WikiPagesContainer #' + value + ' .WikiPageItem:selected').val();
    var pagename = $('#WikiPagesContainer #' + value + ' .WikiPageItem:selected').text();
    $('#WikiPageIdSelection').text(pageid);
    $('#WikiPageLink').val(pagename);
}

function onNamespaceChanged() {
    $('#WikiPageIdSelection').text('');
    $('#WikiPageLink').val('');

    $('#WikiPagesContainer .WikiPagesList').hide();
    var value = $('#Namespaces').val();
    $('#WikiPagesContainer #' + value).show();

    var pageid = $('#WikiPagesContainer #' + value + ' .WikiPageItem:selected').val();
    var pagename = $('#WikiPagesContainer #' + value + ' .WikiPageItem:selected').text();
    $('#WikiPageIdSelection').text(pageid);
    $('#WikiPageLink').val(pagename);
}   